openerp.product_card = function(instance) {
    var QWeb = instance.web.qweb,
        _t = instance.web._t;

    instance.web.product_card = instance.web.product_card || {};

    instance.web.views.add('tree_product_card_issue', 'instance.web.product_card.IssueListView');
    instance.web.product_card.IssueListView = instance.web.ListView.extend({
        init: function() {
            var self = this;
            this._super.apply(this, arguments);
            this.on('list_view_loaded', this, function() {
                if(self.__parentedParent.$el.find('.oe_product_card_issue').length == 0){
                    var button1 = $("<button type='button' class='oe_button oe_product_card_issue'>卡券发行</button>")
                        .click(this.proxy('product_card_issue_action'));
                    self.__parentedParent.$el.find('.oe_list_buttons').append(button1);
                    var button2 = $("<button type='button' class='oe_button oe_product_card_in'>批量入库</button>")
                        .click(this.proxy('product_card_in_action'));
                    self.__parentedParent.$el.find('.oe_list_buttons').append(button2);
                    var button3 = $("<button type='button' class='oe_button oe_product_card_disable'>批量禁用</button>")
                        .click(this.proxy('product_card_disable_action'));
                    self.__parentedParent.$el.find('.oe_list_buttons').append(button3);
                    var button4 = $("<button type='button' class='oe_button oe_product_card_enable'>批量启用</button>")
                        .click(this.proxy('product_card_enable_action'));
                    self.__parentedParent.$el.find('.oe_list_buttons').append(button4);
                     var button5 = $("<button type='button' class='oe_button oe_product_card_active'>批量激活</button>")
                         .click(this.proxy('product_card_active_action'));
                    self.__parentedParent.$el.find('.oe_list_buttons').append(button5);
                     var button6 = $("<button type='button' class='oe_button oe_product_card_deactive'>批量取消激活</button>")
                         .click(this.proxy('product_card_deactive_action'));
                     self.__parentedParent.$el.find('.oe_list_buttons').append(button6);
                    var button7 = $("<button type='button' class='oe_button oe_product_card_discard'>批量销毁</button>")
                         .click(this.proxy('product_card_discard_action'));
                     self.__parentedParent.$el.find('.oe_list_buttons').append(button7);

                }
            });
        },
        product_card_issue_action: function () {
            var self = this;
            var action_manager = new instance.web.ActionManager(self);

            return action_manager.do_action('product_card.product_card_produce_action', {
                        on_close: function () {
                            self.do_search(self.last_domain, self.last_context, self.last_group_by);
                        }
            });
        },

        product_card_in_action: function () {
            var self = this;
            var action_manager = new instance.web.ActionManager(self);

            return action_manager.do_action('product_card.product_card_in_action', {
                        on_close: function () {
                            self.do_search(self.last_domain, self.last_context, self.last_group_by);
                        }
            });

        },

        product_card_enable_action: function () {
            var self = this;
            var action_manager = new instance.web.ActionManager(self);

            return action_manager.do_action('product_card.product_card_enable_action', {
                        on_close: function () {
                            self.do_search(self.last_domain, self.last_context, self.last_group_by);
                        }
            });

        },

        product_card_disable_action: function () {
            var self = this;
            var action_manager = new instance.web.ActionManager(self);

            return action_manager.do_action('product_card.product_card_disable_action', {
                        on_close: function () {
                            self.do_search(self.last_domain, self.last_context, self.last_group_by);
                        }
            });

        },

        product_card_active_action: function () {
            var self = this;
            var action_manager = new instance.web.ActionManager(self);

            return action_manager.do_action('product_card.product_card_active_action', {
                        on_close: function () {
                            self.do_search(self.last_domain, self.last_context, self.last_group_by);
                        }
            });

        },
        product_card_deactive_action: function () {
            var self = this;
            var action_manager = new instance.web.ActionManager(self);

            return action_manager.do_action('product_card.product_card_deactive_action', {
                        on_close: function () {
                            self.do_search(self.last_domain, self.last_context, self.last_group_by);
                        }
            });

        },
        product_card_discard_action: function () {
            var self = this;
            var action_manager = new instance.web.ActionManager(self);

            return action_manager.do_action('product_card.product_card_discard_action', {
                        on_close: function () {
                            self.do_search(self.last_domain, self.last_context, self.last_group_by);
                        }
            });

        },

        //重载列表数据
        do_search: function(domain, context, group_by) {
            var self = this;
            this.last_domain = domain;
            this.last_context = context;
            this.last_group_by = group_by;
            this.old_search = _.bind(this._super, this);
            return self.old_search(new instance.web.CompoundDomain(self.last_domain), self.last_context, self.last_group_by);
        },
    });

    $(function(){
        instance.web.views.add('graph_view_card_day_stat', 'instance.web.product_card.DayStatGraph');
        instance.web.product_card.DayStatGraph = instance.web_graph.GraphView.extend({
            init: function() {
                var self = this;
                this._super.apply(this, arguments);
                this.on('list_view_loaded', this, function() {

                });
                //统计图表上方添加按钮
                if(self.__parentedParent.$el.find('.oe_product_card_today_stat').length == 0){
                    var button1 = $("<button type='button' class='oe_button oe_product_card_today_stat'>刷新当天订单统计</button>")
                        .click(this.proxy('product_card_today_stat_action'));
                    self.__parentedParent.$el.find('.oe_view_manager_buttons').append(button1);
                }
            },
            product_card_today_stat_action: function(){
                var self = this;
                this.model.call('summary_today', []).then(function(){
                    self.search_view.do_search();
                })
            }
        });
    })
}
